<template>
  <div class="app-container search-table-box aidex-table">
    <el-card shadow="never" ref="queryRef" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
      <div class="filter-container">
        <div class="search_box">
          <el-form :model="queryParams" ref="queryForm" label-width="80px" size="mini">
            <el-row :gutter="16">
              <el-col :md="6" >
                <el-form-item label="公司代码" prop="companycode">
                  <el-input
                    v-model="queryParams.companycode"
                    placeholder="请输入公司代码"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" >
                <el-form-item label="公司名称" prop="companyname">
                  <el-input
                    v-model="queryParams.companyname"
                    placeholder="请输入公司名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" >
                <el-form-item label="打印抬头" prop="printtitle">
                  <el-input
                    v-model="queryParams.printtitle"
                    placeholder="请输入打印抬头"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="WMS前缀" prop="wmsbillprefix">
                  <el-input
                    v-model="queryParams.wmsbillprefix"
                    placeholder="请输入WMS前缀"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="是否有效" prop="isenable">
                  <el-select v-model="queryParams.isenable" placeholder="请选择是否有效" @change="handleQuery" clearable style="width: 100%;">
                    <el-option
                      v-for="dict in dict.type.sys_normal_enable"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="添加人" prop="addcname">
                  <el-input
                    v-model="queryParams.addcname"
                    placeholder="请输入添加人"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="添加时间" prop="adddatetime">
                  <el-date-picker clearable
                    v-model="queryParams.adddatetime"
                    type="date"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择添加时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="修改人" prop="modifycname">
                  <el-input
                    v-model="queryParams.modifycname"
                    placeholder="请输入修改人"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" v-if="advanced">
                <el-form-item label="修改时间" prop="modifydatetime">
                  <el-date-picker clearable
                    v-model="queryParams.modifydatetime"
                    type="date"
                    style="width: 100%"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择修改时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :md="6" align="right" style="float: right;">
                <el-form-item>
                  <el-button class="filter-item" type="primary" @click="handleQuery">搜索</el-button>
                  <el-button class="filter-item" style="margin-left: 8px" @click="resetQuery">重置</el-button>
                  <a @click="toggleAdvanced" style="margin:0 4px 0 8px ;vertical-align: middle;">
                    {{ advanced ? '收起' : '展开' }}
                    <i :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
                  </a>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" >
      <template #header>
        <el-row>
          <el-col :span="8">
            <div class="card-header">
              <el-button disabled type="text">公司信息</el-button>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="btn_box" align="right" style="float: right;">
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                type="primary"
                icon="el-icon-plus"
                @click="handleAdd"
                size="mini"
                v-hasPermi="['core:company:add']"
              >新增</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-delete"
                type="danger"
                :disabled="multiple"
                @click="handleDelete"
                size="mini"
                v-if="!multiple"
                v-hasPermi="['core:company:remove']"
              >删除</el-button>
              <el-button
                class="filter-item"
                style="margin-left: 8px;"
                icon="el-icon-download"
                @click="handleExport"
                size="mini"
                v-hasPermi="['core:company:export']"
              >导出</el-button>
              <right-toolbar :showSearch.sync="showSearch" @showSearchFun="showSearchFun" @queryTable="getList"></right-toolbar>
            </div>
          </el-col>
        </el-row>
      </template>

      <el-table
              border
              stripe
              default-expand-all
              ref="tableRef"
              v-loading="loading"
              :data="companyList"
              @selection-change="handleSelectionChange"
              :row-style="rowClass"
              :header-cell-style= "{
                'font-size': '14px !important',
                'font-weight': '500 !important',
                'background-color':'rgb(241 241 241 / 52%)'
              }"
              highlight-current-row style="width: 100%;"
              :height="tableHeight"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="公司代码" align="center" prop="companycode" />
        <el-table-column label="公司名称" align="center" prop="companyname" />
        <el-table-column label="打印抬头" align="center" prop="printtitle" />
        <el-table-column label="WMS前缀" align="center" prop="wmsbillprefix" />
        <el-table-column label="是否有效" align="center" prop="isenable">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_enable" :value="scope.row.isenable"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="添加人" align="center" prop="addcname" />
        <el-table-column label="添加时间" align="center" prop="adddatetime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.adddatetime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改人" align="center" prop="modifycname" />
        <el-table-column label="修改时间" align="center" prop="modifydatetime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modifydatetime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['core:company:edit']"
            >修改</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              style="color: red;"
              @click="handleDelete(scope.row)"
              v-hasPermi="['core:company:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <svg-icon icon-class="search-none" style="font-size: 64px;" />
          <p>暂无数据</p>
        </div>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改公司对话框 -->
      <el-dialog v-dialogDrag :close-on-click-modal="false"  :title="title" :visible.sync="open" width="720px" top="10vh" append-to-body>
        <div style="height: 490px;overflow: auto; padding: 12px 24px;">
          <el-form ref="form" size="mini" :model="form" :rules="rules" label-position="top">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="公司名称" prop="companyname">
                  <el-input v-model="form.companyname" placeholder="请输入公司名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="打印抬头" prop="printtitle">
                  <el-input v-model="form.printtitle" placeholder="请输入打印抬头" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="WMS前缀" prop="wmsbillprefix">
                  <el-input v-model="form.wmsbillprefix" placeholder="请输入WMS前缀" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否有效" prop="isenable">
                  <el-select v-model="form.isenable" placeholder="请选择是否有效" clearable style="width: 100%;">
                    <el-option
                      v-for="dict in dict.type.sys_normal_enable"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
          <el-button size="mini" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { listCompany, getCompany, delCompany, addCompany, updateCompany } from "@/api/core/company";

export default {
  name: "Company",
  dicts: ['sys_normal_enable'],
  data() {
    return {
      advanced: false,
      tableHeight: this.getInitTableHeight(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companycode: null,
        companyname: null,
        printtitle: null,
        wmsbillprefix: null,
        isenable: null,
        addcname: null,
        adddatetime: null,
        modifycname: null,
        modifydatetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyname: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        printtitle: [
          { required: true, message: "打印抬头不能为空", trigger: "blur" }
        ],
        wmsbillprefix: [
          { required: true, message: "WMS前缀不能为空", trigger: "blur" }
        ],
        isenable: [
          { required: true, message: "是否有效不能为空", trigger: "change" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 行样式 */
    rowClass({ row, rowIndex }) {
      if (this.ids.includes(row.id)) {
          return { background: "#d9edf7", height:'20px' };
      } else {
          return { height:'20px'};
      }
    },
   /** 隐藏搜索按钮操作 */
   showSearchFun(isShowSearch){
      this.showSearch = isShowSearch
      let oldHeight = this.$refs.queryRef.$el.offsetHeight
      if(!isShowSearch){
          //当前是显示状态
          oldHeight = oldHeight + 12
      }else{
          oldHeight = oldHeight - 12
      }
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 展开按钮操作 */
    toggleAdvanced() {
      const oldHeight = this.$refs.queryRef.$el.offsetHeight
      this.advanced = !this.advanced
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 查询公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(response => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companycode: null,
        companyname: null,
        printtitle: null,
        wmsbillprefix: null,
        isenable: null,
        remark: null,
        addcname: null,
        adddatetime: null,
        modifycname: null,
        modifydatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companycode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companycode = row.companycode || this.ids
      getCompany(companycode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companycode != null) {
            updateCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companycodes = row.companycode || this.ids;
      this.$modal.confirm('是否确认删除公司编号为"' + companycodes + '"的数据项？').then(function() {
        return delCompany(companycodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('core/company/export', {
        ...this.queryParams
      }, `company_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
